package com.azkj.album;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;

import androidx.multidex.MultiDexApplication;

import com.azkj.album.network.Constants;
import com.azkj.album.network.NetworkMaster;
import com.bytedance.sdk.open.douyin.DouYinOpenApiFactory;
import com.bytedance.sdk.open.douyin.DouYinOpenConfig;
import com.tencent.mmkv.MMKV;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import java.io.File;


public class MyApplication extends MultiDexApplication {

    private static Application application;
    private static Activity current;

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;

        String dir = new File("/data/data/" + getPackageName() + "/shared_prefs").getAbsolutePath() + "/mmkv";
        MMKV.initialize(dir);
        NetworkMaster.init(this, BuildConfig.BUILD_TYPE);
        initLifecycle();
        init3rd();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
//            StrictMode.setVmPolicy(builder.build());
//            try {
//                Method ddfu = StrictMode.class.getDeclaredMethod("disableDeathOnFileUriExposure");
//                ddfu.invoke(null);
//
//            } catch (Exception e) {
//
//            }

//        }

    }

    private void init3rd() {

        DouYinOpenApiFactory.init(new DouYinOpenConfig(Constants.DOU_YIN));

        //解决android N（>=24）系统以上分享 路径为file://时的 android.os.FileUriExposedException异常
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            builder.detectFileUriExposure();
        }
        if (getSharedPreferences(Constants.SP_NAME, Context.MODE_PRIVATE).getBoolean("is_first", false)) {
            if (BuildConfig.BUILD_TYPE.equals("release")) {
                UMConfigure.init(this, Constants.UMENG_KEY, Constants.UMENG_CHANNEL, UMConfigure.DEVICE_TYPE_PHONE, "");
                MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);

//                DouYinOpenApiFactory.init(new DouYinOpenConfig(Constants.DOU_YIN));

            }
        }


    }

    private void initLifecycle() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {
                current = activity;
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }


    public static Application getApplication() {
        return application;
    }

    public static Activity getCurrentActivity() {
        return current;
    }


    @Override
    public void onTerminate() {
        super.onTerminate();
        Log.d("MyApplication", "onTerminate");
    }
}
